//
//  YGProductHeaderView.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/28.
//

#import "YGProductHeaderView.h"
#import "YGSelectButton.h"
@interface YGProductHeaderView ()
@property (nonatomic, strong) NSArray *datas;

@end
@implementation YGProductHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView{
    if (self.datas.count == 0) {
        return;
    }
    
    CGFloat w = self.width/self.datas.count;
    CGFloat x = 0;
    CGFloat height = self.height;
    for (int i = 0; i < self.datas.count; i++) {
        YGSelectButton *btn = [[YGSelectButton alloc] init];
        NSString *title = [self.datas objectAtIndex:i];
//        NSString *title = [dic objectForKey:@"title"];
        [btn setTitle:title forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(x, 0, w, height);
        x += w;
        [self addSubview:btn];
    }
}

- (void)configureData:(NSArray *)datas{
    self.datas = datas;
    [self initView];
}

- (void)reloadData:(NSArray *)datas{
    for (YGSelectButton *btn in self.subviews) {
        if ([btn isKindOfClass:YGSelectButton.class]) {
            NSInteger tag = btn.tag;
            [btn setTitle:[datas objectAtIndex:tag] forState:UIControlStateNormal];
        }
    }
    self.datas = datas;
    [self layoutSubviews];
}

- (void)clickBtnAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(headerViewClickAction:)]) {
        [self.delegate headerViewClickAction:sender.tag];
    }
}

- (NSArray *)getHeaderTitles{
    return self.datas;
}
@end
